package com.yphone.ui.pay.presenter;

import com.yphone.net.IUIDataListener;
import com.yphone.net.bean.BaseResultWrapper;
import com.yphone.tools.MyLog;
import com.yphone.tools.ToastUtil;
import com.yphone.ui.dialog.CommSmallLoadingDialog;
import com.yphone.ui.pay.bean.CheckOrderInfo;
import com.yphone.ui.pay.contract.OrderContract;
import com.yphone.ui.pay.model.OrderModel;

import org.json.JSONException;
import org.json.JSONObject;

public class OrderPresenter implements OrderContract.IOrderPresenter {


    private final OrderContract.IView iView;
    private OrderModel orderModel;
    private boolean exitFlag;
    private boolean pauseFlag;

    public OrderPresenter(OrderContract.IView iView) {
        this.iView = iView;
        iView.setPresenter(this);
    }

    @Override
    public void getQRCode(String serverId, String orderNo,int num) {
        orderModel.getQRCode(new IUIDataListener() {
            @Override
            public void uiDataError(Exception exc) {
                CommSmallLoadingDialog.dismissDialog();
            }

            @Override
            public void uiDataSuccess(Object obj) {
                BaseResultWrapper<String> wrapper = (BaseResultWrapper<String>) obj;
                if (wrapper.code == 1) {
                    iView.updateQRCode(wrapper.data);
                }else {
                    iView.getOrderError(wrapper.msg);
                }
                CommSmallLoadingDialog.dismissDialog();

            }
        }, serverId, orderNo,num);
    }

    @Override
    public void checkOrderState(final String orderNo) {
        if (exitFlag){
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                orderModel.checkOrderState(new IUIDataListener() {
                    @Override
                    public void uiDataError(Exception exc) {
                        pauseFlag = false;
                        CommSmallLoadingDialog.dismissDialog();
                    }

                    @Override
                    public void uiDataSuccess(Object obj) {

                        BaseResultWrapper<CheckOrderInfo> wrapper = (BaseResultWrapper<CheckOrderInfo>) obj;
                        MyLog.e("pauseFlag = "+pauseFlag+"  code = "+wrapper.code);
                        if (wrapper.code == 1){
                            CheckOrderInfo orderInfo = wrapper.data;
                            if ("SUCCESS".equals(orderInfo.state)) {
                                exitFlag = true;
                                iView.paySuccess();
                                return;
                            }
                        }
                        checkOrderState(orderNo);
                        CommSmallLoadingDialog.dismissDialog();
                    }
                }, orderNo);
            }
        }).start();
    }

    @Override
    public void destory() {
        exitFlag = true;
    }

    @Override
    public void start() {
        orderModel = new OrderModel();
    }
}
